package bpm.service.impl.deptagenda;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import bpm.dao.deptagenda.AgendaLogDao;
import bpm.domain.deptagenda.AgendaLog;
import bpm.domain.user.OrgGroup;
import bpm.domain.user.OrgUser;
import bpm.service.deptagenda.AgendaLogServcie;
import bpm.service.user.OrgGroupService;
import bpm.util.UUID;

@Service("agendaLogServcie")
@Transactional
public class AgendaLogServiceBean implements AgendaLogServcie {
	@Resource private OrgGroupService orgGroupService;
	@Resource private AgendaLogDao agendaLogDao;
	
	
	public void add(AgendaLog log) {
		//1 id operTime
		String id = UUID.id("DA", "LOG");
		String operTime = String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL", new Date());
		log.setLogId(id);
		log.setOperTime(operTime);
		
		String sql = "insert into BPM_DA_AGENDALOG (LOGID, USERACCOUNT, USERNAME, DEPTCODE, DEPTNAME, LOGINIP,"
			+" DETAIL, OPERTIME, TYPE, RESULT) values (?,?,?,?,?,?,?,?,?,?)";
		
		agendaLogDao.update(sql, 
				log.getLogId(),
				log.getUserAccount(),
				log.getUserName(),
				log.getDeptCode(),
				log.getDeptName(),
				log.getLoginIp(),
				log.getDetail(),
				log.getOperTime(),
				log.getType(),
				log.getResult()
		);
	}

	public void add(List<AgendaLog> logs) {
		// TODO Auto-generated method stub
		
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public AgendaLog get(Map<String, Object> param, OrgUser register) {
		String ip = "";
		if(param.get("ip")!=null)
			ip= (String)param.get("ip");
		String userAccount = register.getId();
		String userName = register.getName();
		String deptCode = register.getOrgGroup().getId();
		String deptName = "";
		OrgGroup group = orgGroupService.findUniqueDeptById(deptCode);
		if(group!=null)
			deptName = group.getName();
		
		return new AgendaLog(userAccount,userName,deptCode,deptName,ip);
	}
}
